package com.reservationserver.service.impl.admin;

import com.reservationpojo.domin.vo.AdminBadmintonvo;
import com.reservationpojo.entity.Badminton;
import com.reservationpojo.entity.shutdowncourt;
import com.reservationpojo.sendmessageevent.Sse;
import com.reservationserver.mapper.admin.AdminBadmintonMapper;
import com.reservationserver.service.admin.AdminBadmintonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class AdminBadmintonServiceimpl implements AdminBadmintonService {

    private final AdminBadmintonMapper adminBadmintonMapper;

    private final RedisTemplate redisTemplate;
    /**
     * 查询任意预约记录
     * @param badminton
     * @return
     */
    @Override
    public List<Badminton> selectreservation(AdminBadmintonvo badminton) {
        log.info("查询预约记录");
        badminton.setPage((badminton.getPage()-1)*10);
        return adminBadmintonMapper.selectreservation(badminton);
    }
    /**
     * 取消预约 删除redis中的二维码同时通知该用户 并修改预约记录的状态为已取消
     * @param badminton uid,starttime,endtime,position,createtime
     *ce
     */
    @Override
    public void deleteReservation(Badminton badminton) {
        log.info("管理员取消预约");
        badminton.setStatus("已取消");
        String message ="您的预约已被管理员取消";
        Sse.sendMessage("badminton"+badminton.getReservedByid(),message);
        redisTemplate.delete("badminton"+badminton.getCreateTime()+badminton.getReservedByid());
        adminBadmintonMapper.deleteReservation(badminton);
    }
    /**
     * 签到 设置对应的预约记录为已签到 删除redis中的二维码门票
     * @param badminton
     */
    @Override
    public Badminton checkReservation(Badminton badminton) {
        log.info("管理员扫码入场");
        badminton.setStatus("已签到");
        redisTemplate.delete(badminton.getReservedByid());
        return adminBadmintonMapper.checkReservation(badminton);
    }
    /**
     * 标识为羽毛球场地
     * @param courtId 改为"badminton"courtId
     */
    @Override
    public void shutDownCourt(String courtId, LocalDateTime starttime) {
        log.info("关闭指定场地");
        courtId="badminton"+courtId;
        adminBadmintonMapper.shutDownCourt(courtId,starttime);
    }
    @Override
    public void openCourt(String courtId) {
        courtId="badminton"+courtId;
        adminBadmintonMapper.openCourt(courtId);
    }
    @Override
    public List<shutdowncourt> checkCourt() {
        List<shutdowncourt> list=adminBadmintonMapper.checkCourt();
        List<shutdowncourt> flag=new ArrayList<>();
        if(!list.isEmpty()){
            for (shutdowncourt s : list) {
                s.setCourt(String.valueOf(s.getCourt().charAt(9)));
                flag.add(s);
            }
        }
        return flag;
    }
}
